//
//  YTFVisitor.m
//  weibo_yoo_OC
//
//  Created by pro on 16/2/27.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "YTFVisitor.h"
#import <Masonry.h>

@interface YTFVisitor ()
//首页动画图片
@property(nonatomic,strong)UIImageView *homeIconImageView;
//通用图片
@property(nonatomic,strong)UIImageView *iconImageView;
//提示语
@property(nonatomic,strong)UILabel *messageLabel;
//注册按钮
@property(nonatomic,strong)UIButton *registerBtn;
//登录按钮
@property(nonatomic,strong)UIButton *loginBtn;
//遮罩视图
@property(nonatomic,strong)UIImageView *maskImageView;

@end

@implementation YTFVisitor

//自定义视图
//第一步：重写init
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithWhite:237/255.0 alpha:1];
        
        //调用私有方法，添加视图并设置约束
        [self setupUI];
    }
    return self;
}


//第二步：定义一个私有方法，添加视图并且设置约束
-(void)setupUI{
    //添加控件
    [self addSubview:self.homeIconImageView];
    [self addSubview:self.iconImageView];
    [self addSubview:self.messageLabel];
    [self addSubview:self.registerBtn];
    [self addSubview:self.loginBtn];
    [self addSubview:self.maskImageView];
    
    //添加约束
    __weak typeof(self) weakSelf = self;
    //旋转动画居中对齐
    self.homeIconImageView.center = weakSelf.center;
    
    //通用图片约束
    self.iconImageView.center = weakSelf.center;
    
    //提示约束
    [self.messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.homeIconImageView.mas_bottom).with.offset(10);
        make.centerX.equalTo(weakSelf.homeIconImageView.mas_centerX);
        make.width.equalTo(@216);
    }];
    
    
    //注册按钮的约束
    [self.registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.messageLabel.mas_bottom);
        make.left.equalTo(weakSelf.messageLabel.mas_left);
        make.width.equalTo(@100);
        make.height.equalTo(@30);
    }];
    
    
    //登录按钮的约束
    [self.loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.messageLabel.mas_bottom);
        make.right.equalTo(weakSelf.messageLabel.mas_right);
        make.width.equalTo(@100);
        make.height.equalTo(@30);
    }];
    
    //遮罩视图约束
    self.maskImageView.center = weakSelf.center;
    
    //开启动画
    [self startAnimation];
    
}


//对外的一个方法，用来给不同的控制器上的访客视图赋值
-(void)setInfoWithMessage:(NSString *)message andWithImage:(NSString *)image andWithAnimation:(BOOL)animation{
    self.messageLabel.text = message;
    self.iconImageView.image = [UIImage imageNamed:image];
    self.homeIconImageView.hidden = animation;
}

//添加动画
-(void)startAnimation{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    
    animation.toValue = [NSNumber numberWithFloat:M_2_PI * 2];
    animation.repeatCount = MAXFLOAT;
    animation.duration = 5;
    
    //切换视图的时候，保证动画不停止
    animation.removedOnCompletion = NO;
    
    //添加
    [self.homeIconImageView.layer addAnimation:animation forKey:nil];
    
}


//懒加载
//首页动画图片
-(UIImageView *)homeIconImageView{
    if (_homeIconImageView == nil) {
        _homeIconImageView = [[UIImageView alloc]init];
        _homeIconImageView.image = [UIImage imageNamed:@"visitordiscover_feed_image_smallicon"];
        [_homeIconImageView sizeToFit];
    }
    return _homeIconImageView;
}

//通用图片
-(UIImageView *)iconImageView{
    if (_iconImageView == nil) {
        _iconImageView = [[UIImageView alloc]init];
        _iconImageView.image = [UIImage imageNamed:@"visitordiscover_feed_image_house"];
        [_iconImageView sizeToFit];
    }
    return _iconImageView;
}


//提示语
-(UILabel *)messageLabel{
    if (_messageLabel == nil) {
        _messageLabel = [[UILabel alloc]init];
        _messageLabel.font = [UIFont systemFontOfSize:14];
        _messageLabel.textColor = [UIColor grayColor];
        _messageLabel.textAlignment = NSTextAlignmentCenter;
        _messageLabel.numberOfLines = 0;
        _messageLabel.text = @"关注一些人，回这里看看有什么惊喜";
    }
    return _messageLabel;
}


//注册按钮
-(UIButton *)registerBtn{
    if (_registerBtn == nil) {
        _registerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_registerBtn setBackgroundImage:[UIImage imageNamed:@"common_button_white_disable"] forState:UIControlStateNormal];
        [_registerBtn setTitle:@"注册" forState:UIControlStateNormal];
        [_registerBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        [_registerBtn addTarget:self action:@selector(registerBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _registerBtn;
}


//登录按钮
-(UIButton *)loginBtn{
    if (_loginBtn == nil) {
        _loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_loginBtn setBackgroundImage:[UIImage imageNamed:@"common_button_white_disable"] forState:UIControlStateNormal];
        [_loginBtn setTitle:@"登录" forState:UIControlStateNormal];
        [_loginBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        [_loginBtn addTarget:self action:@selector(loginBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _loginBtn;
}


//遮罩视图
-(UIImageView *)maskImageView{
    if (_maskImageView == nil) {
        _maskImageView = [[UIImageView alloc]init];
        _maskImageView.image = [UIImage imageNamed:@"visitordiscover_feed_mask_smallicon"];
        [_maskImageView sizeToFit];
    }
    return _maskImageView;
}


-(void)registerBtnClick{
    if ([self.delegate respondsToSelector:@selector(registerClick)]) {
        [self.delegate registerClick];
    }
}


-(void)loginBtnClick{
    if ([self.delegate respondsToSelector:@selector(loginClick)]) {
        [self.delegate loginClick];
    }
}


@end
